package com.infore.dataReceiving.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_SMALL_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String DATE_SMALL_YMDH = "yyyy-MM-dd HH:00"; 
	public static final String DATE_SMALL_YMDHH = "yyyy-MM-dd HH"; 
	public static final String DATE_SMALL_YMDHMS = "yyyy-MM-dd HH:00:00"; 
	public static final String DATE_SMALL_STR  = "yyyy-MM-dd";
	public static final String DATE_SMALL_YM  = "yyyy-MM";
	public static final String DATE_ALL_STR  = "yyyyMMddHHmmss";
    /** 
     * 获取当前日期 
     * @return 
     */  
    public static String getCurrentDate(String patten){  
        SimpleDateFormat format = new SimpleDateFormat(patten);        
        return format.format(new Date());  
    }  
    
    /**
     * 当月最后一天
     * @return
     */
	public static String getLastDayM(Date d,String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);  
        Date theDate = calendar.getTime();
        return df.format(theDate);
	}
	
	/**
     * 当月最后一天(含小时),适用于年月
     * @return
     */
	public static Date getLastDayMonthM(Date d,String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);  
        Date theDate = calendar.getTime();
        return theDate;
	}
	
	/**
     * 当月最后一天(含小时),适用于年月日
     * @return
     */
	public static Date getLastDayDayM(Date d,String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);  
        Date theDate = calendar.getTime();
        return theDate;
	}
	
	/** 
     *  
     * 小时加减
     */  
    public static String addHour(Date d,int n,String pattern){  
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(d);
        cal.add(Calendar.HOUR,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    } 
    
    public static String addHour(String d,int n,String pattern){  
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance(); 
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.HOUR,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }
      
    /** 
     *  
     * 日期加减
     */  
    public static String addDate(Date d,int n,String pattern){  
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(d);
        cal.add(Calendar.DATE,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    } 
    
    public static String addDate(String d,int n,String pattern){  
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance(); 
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.DATE,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    } 
    
    /** 
     *  
     * 月份加减
     */  
    public static String addMonth(Date d,int n,String pattern){  
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(d);
        cal.add(Calendar.MONTH,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }  
    public static String addMonth(String d,int n,String pattern){  
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance(); 
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.MONTH,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    } 
    /** 
     *  
     * 年份加减
     */  
    public static String addYear(Date d,int n,String pattern){  
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(d);
        cal.add(Calendar.YEAR,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }  
    public static String addYear(String d,int n,String pattern){  
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance(); 
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.YEAR,n);  
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    } 
    
    /**
	 * 
	 * 分钟加减
	 */
	public static String addMin(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.MINUTE, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}
	/**
	 * 
	 * 分钟加减
	 */
	public static Date addYear(Date d, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, n);
		Date datNew = cal.getTime();
		return datNew;
	}
    
    /** 
     * 日期转换成字符串 
     * @param date 
     * @return 
     */  
    public static String dateToString(Date date,String pattern){  
        SimpleDateFormat format = new SimpleDateFormat(pattern);  
        return format.format(date);  
    }
    

    /** 
     * 日期转换成字符串 年 月 日
     * @param date 
     * @return 
     */  
    public static String dateToStringYY(Date date){  
        SimpleDateFormat format = new SimpleDateFormat(DATE_SMALL_STR);
        String d=format.format(date);
        d=d.substring(0,4)+"年"+d.substring(5,7)+"月"+d.substring(8)+"日";
        return d;
    }
    
    /** 
     * 字符串转换日期 
     * @param str 
     * @return 
     */  
    public static Date stringToDate(String str,String pattern){  
        SimpleDateFormat format = new SimpleDateFormat(pattern);  
        if(!str.equals("")&&str!=null){  
            try {  
                return format.parse(str);  
            } catch (ParseException e) {  
                e.printStackTrace();  
            }  
        }  
        return null;  
    }  
    
    /**
     * 查询当月天 列表 不包含今天
     * @return ["03-01","03-02"....]
     */
   public  static List<String>  dangyueDayStringList(){
	   List<String> list  = new ArrayList<String>();
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(new Date());
	   int count = ca.get(Calendar.DAY_OF_MONTH); 
	   ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH)); 
	  
	   SimpleDateFormat si = new SimpleDateFormat("MM-dd");
	   String time ="";
	   int temp =1;
	   while( temp<count){
		   time = si.format(ca.getTime());
		   list.add(time);
		   ca.add(Calendar.DAY_OF_MONTH, 1);
		  
		   temp++;
	   }
	   return list;
   }
   
   /**
    * 日期第一天  20
    * @param date
    * @return 2017-03-01
    */
   public static String dangyueDiyitian(Date date,String pattern){
	   SimpleDateFormat si = new SimpleDateFormat(pattern);
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00); 
	   return si.format(ca.getTime());
   }
   
   /**
    * 日期第一天(含小时),只适用于年月
    * @param date
    * @return 2017-03-01
    */
   public static Date dangyueDiyitianMonth(Date date,String pattern){
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00); 
	   return ca.getTime();
   }
   
   /**
    * 日期第一天(含小时),只适用于年月日
    * @param date
    * @return 2017-03-01
    */
   public static Date dangyueDiyitianDay(Date date,String pattern){
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.get(Calendar.DAY_OF_MONTH), 00, 00, 00); 
	   return ca.getTime();
   }
   
   /**
    * *
    * @param args
    */
   public static Date addDate2(Date date,int n){
	   Calendar cal = Calendar.getInstance(); 
       cal.setTime(date);
       cal.add(Calendar.DATE,n);  
       Date datNew = cal.getTime();
	   return datNew;
   } 
   
	/**
	 * 获取年份
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取年份2
	 * @param date
	 * @return
	 */
	public static int getYear(String date) {
		Date d= stringToDate(date, DATE_ALL_STR);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.YEAR);
	}
	
	/**
	 * 计算时长相差小时
	 * 
	 * @author 蔡永顺
	 * @return int
	 * @param t1
	 * @param t2
	 * @return
	 * @date 2019年12月11日上午10:11:04
	 */
	public static int diffHours(Date t1, Date t2) {
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = t2.getTime() - t1.getTime();
	    // 计算差多少天
	    int day = (int) (diff / nd);
	    // 计算差多少小时
	    int hour = (int) (diff % nd / nh) + day *24;
	    return hour;
	}
	
	/**
	 * 获取天数
	 * @param date
	 * @return
	 */
	public static int getDAY_OF_YEAR(String date,String pattern) {
		Date d= stringToDate(date, pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.DAY_OF_YEAR);
	}
	
	/**
	 * 获取月份
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);
	}
	
	/**
	 * 获取月份
	 * @param date
	 * @return
	 */
	public static int getDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取一年起始时间
	 * @param date
	 * @return
	 */
	public static Date getFirstDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	public static String getFirstDay_Year(Date date,String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
	    return format.format(datNew);
	}
	/**
	 * 获取一年结束时间
	 * @param date
	 * @return
	 */
	public static Date getFinalyDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	
	public static String getFinalyDay_Year(Date date,String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
	    return format.format(datNew);
	}
	
	public static String getFirstDay_Month(String date,String pattern,String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int firstDat = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, firstDat);
		cal.set(Calendar.HOUR_OF_DAY, 00);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
	    return new SimpleDateFormat(pattern2).format(cal.getTime());
	}
	public static String getFinalyDay_Month(String date,String pattern,String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
	    return new SimpleDateFormat(pattern2).format(cal.getTime());
	}
	
	/**
	 * 获取时间区间
	 * 
	 * @param d1 开始时间区间
	 * @param d2 结束时间区间
	 * @return 时间区间集合
	 */
	public static List<String> getTimeList(String d1, String d2, String pattern, String type) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance();
		List<String> list = new ArrayList<String>();
		Date st, et;
		long sl, el, length;
		try {
			st = sd.parse(d1);
			et = sd.parse(d2);
			sl = st.getTime();
			el = et.getTime();
			if (type == "hour") {// 以小时为间隔
				length = (el - sl) / (60 * 60 * 1000);
			} else if (type == "day") {// 一天为间隔
				length = (el - sl) / (24 * 60 * 60 * 1000);
			} else {// 一月为间隔
				length = (el - sl) / (30L * 24L * 60L * 60L * 1000L); //必须加L，否则溢出结果为负数
			}
			list.add(sd.format(st));
			for (int i = 0; i < length; i++) {
				c.setTime(st);
				if (type == "hour") {
					c.add(Calendar.HOUR, 1);
					st = c.getTime();
				} else if (type == "day") {
					c.add(Calendar.DAY_OF_YEAR, 1);
					st = c.getTime();
				} else {
					c.add(Calendar.MONTH, 1);
					st = c.getTime();
				}
				list.add(sd.format(st));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	* @Title: getWeek
	* @Description: 根据日期字符串判断是当月第几周
	* @param str
	* @return
	* @throws Exception    参数
	* @return int    返回类型
	* @throws
	 */
	public static int getMonthWeek(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		int week = calendar.get(Calendar.WEEK_OF_MONTH);
		// 第几天，从周日开始
		// int day = calendar.get(Calendar.DAY_OF_WEEK);
		return week;
	 }
	
	//得到开始结束年份之间的所有年份
	public static List<String> getTimeYear(String start,String end){
		int startTime = Integer.valueOf(start);
		int endTime = Integer.valueOf(end);
		List<String> time = new ArrayList<String>();
		if (startTime <= endTime) {
			while(startTime != endTime) {
				time.add(startTime+"");
				startTime ++;
			}
			time.add(endTime+"");
		}
		return time;
	}

	//获取当前系统时间
	public static Date getSystemTime(){
		Date enabledTimeDate = new Date();
	
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			enabledTimeDate= sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return enabledTimeDate;
	}
}
